package com.hjm.project.terminal.library.common;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.hjm.common.api.cq.PublicJgRequest;
import com.hjm.common.api.ls.leshua.LSClient;
import com.hjm.common.utils.EncyptorUtil;
import com.hjm.common.utils.LogUtils;
import com.hjm.common.utils.request.AutoLoadData;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.project.terminal.library.domain.TerminalLibrary;
import com.hjm.project.terminal.library.mapper.TerminalLibraryMapper;
import com.hjm.project.terminal.library.service.ITerminalLibraryService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Component
public class SycnPosCommon {
    @Resource
    private TerminalLibraryMapper terminalLibraryMapper;


    /**
     *同步机器入库渠道
     */
    @Async
    public void syncPos(List<String> list, Map params){
        List<List<String>> partition = Lists.partition(list, 500);
        for (List<String> sn : partition) {
            //同步渠道
            String snStr = String.join(",", sn);
            JSONObject reqBody = new JSONObject();
            reqBody.put("serialno",snStr);                      //sn编号
            reqBody.put("facid",params.get("model"));           //厂商代号
            reqBody.put("model",params.get("term"));            //终端ID
            reqBody.put("source",params.get("source"));         //0-定制机 1-自备机
            reqBody.put("encrypType","1");         //0-定制机 1-自备机
            Map<String, String> reqData = PublicJgRequest.getReqData(reqBody.toJSONString(), AutoLoadData.syncCqStock,true);
            if ("0000".equals(reqData.get("resultCode"))) {
                JSONObject responseInfo = JSONObject.parseObject(reqData.get("responseInfo"));
                //成功机器
                JSONArray success = JSONObject.parseArray(responseInfo.getString("arraySuccess"));
                for (Object o : success) {
                     JSONObject data = JSONObject.parseObject(o.toString());
                     TerminalLibrary tl = new TerminalLibrary();
                     tl.setTmk(data.getString("tmk"));
                     tl.setChannelPosno(data.getString("posno"));
                     tl.setMachineNum(data.getString("sn"));
                     tl.setIsSync(1);
                     tl.setNote("同步成功");
                     terminalLibraryMapper.updateTerminalLibraryInfo(tl);
                }

                //成功机器
                JSONArray error = JSONObject.parseArray(responseInfo.getString("arrayFail"));
                for (Object o : error) {
                    JSONObject data = JSONObject.parseObject(o.toString());
                    TerminalLibrary tl = new TerminalLibrary();
                    tl.setMachineNum(data.getString("sn"));
                    tl.setIsSync(1);
                    String code = data.getString("code");
                    String msg = "";
                    if ("0014".equals(code)) {
                        msg = "已有该终端SN号";
                    } else if ("0015".equals(code)){
                        msg = "终端所属厂商信息不完整";
                    } else if ("0016".equals(code)){
                        msg = "入库失败";
                    } else if ("0017".equals(code)){
                        msg = "划拨失败";
                    }
                    tl.setNote(msg);
                    terminalLibraryMapper.updateTerminalLibraryInfo(tl);
                }


            } else {
                LogUtils.logInfo("请求渠道入库失败, " + snStr);
            }
        }

    }


    /**
     *
     */
    @Async
    public void syncLsPos(List<String> list, Map params){
        for (String sn : list) {
            //同步渠道
            LSClient testLSCkient = new LSClient();
            AjaxResult ajaxResult = testLSCkient.terminalInto(sn,params.get("model").toString());
            if (("301").equals(ajaxResult.get("code").toString())) {
                LogUtils.logInfo("请求渠道入库失败:" + sn);
                continue;
            }

            if (("0").equals(ajaxResult.get("code").toString())) {
                //成功机器
                Object resultData = ajaxResult.get("data");
                LogUtils.logInfo("准备进行回更新:"  + JSONObject.toJSONString(resultData));
                JSONArray successArray = JSONObject.parseArray(resultData.toString());
                for (int i = 0; i < successArray.size(); i++) {
                    JSONObject jsonObject = (JSONObject)successArray.get(i);
                    String machine_num = jsonObject.getString("tusn");
                    String channel_sn = jsonObject.getString("sn");

                    TerminalLibrary tl = new TerminalLibrary();
                    tl.setTmk(new EncyptorUtil().getRandomString(32));
                    tl.setChannelPosno(channel_sn);
                    tl.setMachineNum(machine_num);
                    tl.setIsSync(1);
                    tl.setNote("同步成功");
                    terminalLibraryMapper.updateTerminalLibraryInfo(tl);
                }

            } else if (("500").equals(ajaxResult.get("code").toString())){
                TerminalLibrary tl = new TerminalLibrary();
                tl.setMachineNum(sn);
                tl.setIsSync(1);
                tl.setNote(ajaxResult.get("msg").toString());
                terminalLibraryMapper.updateTerminalLibraryInfo(tl);
            }

            }
        }


    public static void main(String[] args) {
        JSONObject data = new JSONObject();
        data.put("fileName","3428f4adeec44893898cea9f4f69b8b8.jpeg");
        data.put("fileContent","http://q3.itc.cn/images01/20240220/3428f4adeec44893898cea9f4f69b8b8.jpeg");
        data.put("imgType","4");

        Map<String, String> reqData = PublicJgRequest.getReqData(data.toJSONString(), "http://43.240.127.2:10203/sendMerchantImg");
        System.out.printf(reqData.toString());


    }

}
